﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ifunction.Fiddler.Configurator.Control
{
    /// <summary>
    /// Interaction logic for DateTimePicker.xaml
    /// </summary>
    public partial class DateTimePicker : UserControl
    {
        public DateTimePicker()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Loaded event of the Grid control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            for (var i = 0; i < 24; i++)
            {
                this.combo_Hour.Items.Add(new ComboBoxItem
                {
                    Content = i.ToString("00"),
                    Tag = i
                });
            }

            for (var i = 0; i < 60; i++)
            {
                this.combo_Minute.Items.Add(new ComboBoxItem
                {
                    Content = i.ToString("00"),
                    Tag = i
                });
            }

            this.combo_Hour.SelectedIndex = 0;
            this.combo_Minute.SelectedIndex = 0;
        }

        /// <summary>
        /// Gets the picker.
        /// </summary>
        /// <value>The picker.</value>
        public DatePicker Picker { get { return this.datePicker; } }

        /// <summary>
        /// Sets the date time.
        /// </summary>
        /// <param name="value">The value.</param>
        public void SetDateTime(DateTime? value)
        {
            var date = this.datePicker.SelectedDate;

            this.datePicker.SelectedDate = value;
            this.combo_Hour.SelectedValue = value != null ? value.Value.Hour : 0;
            this.combo_Minute.SelectedValue = value != null ? value.Value.Minute : 0;
        }

        /// <summary>
        /// Gets the date time.
        /// </summary>
        /// <returns>System.Nullable{DateTime}.</returns>
        public DateTime? GetDateTime()
        {
            DateTime? result = null;

            var date = this.datePicker.SelectedDate;
            if (date != null)
            {
                result = new DateTime(date.Value.Year, date.Value.Month, date.Value.Day, this.combo_Hour.Text.ToInt(), this.combo_Minute.Text.ToInt(), 0, DateTimeKind.Local);
            }

            return result;
        }

        private void combo_Hour_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }
    }
}
